package com.hqbzl.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

@Slf4j
public class TestCreate {
    public static void t1() {
        final Runnable runnable = () -> {
            log.debug("{}", Thread.currentThread().getId());
        };
        final Thread thread = new Thread(runnable, "t1");
        thread.start();
    }

    public static void t2() {
        new Thread(() -> {
            System.out.println("thread");
            log.debug("{}", Thread.currentThread().getId());
        }, "lthread-1").start();
    }

    public static void t3() {
        try {
            final Callable<String> callable = () -> "result";
            final FutureTask<String> task = new FutureTask<String>(callable);
            final Thread thread = new Thread(task, "t1");
            thread.start();
            final String s = task.get();
            System.out.println("s = " + s);
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

    public static void sleep(long t) {
        try {
            TimeUnit.SECONDS.sleep(t);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void t4() {
        new Thread(() -> {
            while (true) {
                sleep(1);
                System.out.println("t1");
            }
        }, "t1").start();
        new Thread(() -> {
            while (true) {
                sleep(1);
                System.out.println("t2");
            }
        }, "t2").start();
    }

    public static void f5() {

    }

    public static void main2(String[] args) {
        final Thread t1 = new Thread(() -> {
            try {
                log.debug("sleep.........");
                TimeUnit.SECONDS.sleep(4);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        }, "t2");
        t1.start();
        sleep(2);
        log.debug("interrupt");
        t1.interrupt();
        log.debug("打断标记:{}", t1.isInterrupted());
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("线程运行中");
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    System.out.println("线程被中断");
                }
            }
        });
        t1.start();
        sleep(10);
        log.debug("interrupt");
        t1.interrupt();
        //log.debug("打断标记:{}", t1.isInterrupted());
    }
    public static void main1(String[] args) {

        final long id = Thread.currentThread().getId();
        log.debug("{}", id);
        System.out.println("wait");
        //t3();
        //t4();

        // 30-50 每2分钟运行一次
        // 30-50 每2分钟运行一次
        // 当前 28 回退两个到 24  算出下次是 30
        // 当前 30 回退两个是 26  第出下次是 30
        // 当前 32 回退两个是 28  第出下次是 30
    }

}
